﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace ConsoleApp1
{

    internal class HttpService
    {
        private string _url = "http://192.168.1.209:11225/";
        private HttpListener listener;

        public HttpService()
        {
            // 创建 HttpListener 实例
            listener = new HttpListener();
            listener.Prefixes.Add(_url);
        }

        public void Start()
        {
            try
            {
                // 启动 HttpListener
                listener.Start();
                Console.WriteLine("HTTP 服务已启动，监听地址: "+ _url);

                while (true)
                {
                    // 等待客户端请求
                    HttpListenerContext context = listener.GetContext();
                    // 获取请求对象
                    HttpListenerRequest request = context.Request;


                    // 获取响应对象
                    HttpListenerResponse response = context.Response;

                    getCusInfo(response);

                   
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误: {ex.Message}");
            }
            finally
            {
                // 停止 HttpListener
                listener.Stop();
            }
        }


        static async Task getCusInfo(HttpListenerResponse response)
        {
            try
            {
                // 创建 HttpClient 实例
                using (HttpClient client = new HttpClient())
                {
                    // 要请求的 URL
                    string url = "http://192.168.1.190:8050/admin/topInfo?userId=wyq";
                    // 发起 GET 请求并等待响应
                    HttpResponseMessage rsp = await client.GetAsync(url);
                    // 确保请求成功
                    rsp.EnsureSuccessStatusCode();
                    // 读取响应内容
                    string responseBody = await rsp.Content.ReadAsStringAsync();
                    // 输出响应内容
                    //Console.WriteLine(responseBody);


                    Dictionary<string, object> dictMap = JsonSerializer.Deserialize<Dictionary<string, object>>(responseBody);


                    Dictionary<string, string> dic = new Dictionary<string, string>();

                    dic.Add("pPhotoData", dictMap["identityPic"].ToString() ?? "");
                    dic.Add("pName", dictMap["partyName"].ToString() ?? "");
                    dic.Add("pSex", dictMap["gender"].ToString() ?? "");
                    dic.Add("pNation", dictMap["nation"].ToString() ?? "");
                    dic.Add("pBirth", dictMap["bornDay"].ToString() ?? "");
                    dic.Add("pAddress", dictMap["certAddress"].ToString() ?? "");
                    dic.Add("pCertNo", dictMap["certNumber"].ToString() ?? "");
                    dic.Add("pDepartment", dictMap["certOrg"].ToString() ?? "");
                    dic.Add("pEffectData", dictMap["effDate"].ToString() ?? "");
                    dic.Add("pExpire", dictMap["expDate"].ToString() ?? "");
                    dic.Add("SAMID", "5-3-20221115-2218466-2114636013");
                    //dic.Add("PCID", "A1CBAA83DDADC533267D1958E4C7F036BF1B0F9E89AA3D859C529728C84BBB38");
                    dic.Add("PCID", "A1CBAA83DDADC533267D1958E4C7F036" + Code.b(DateTime.Now.ToString(Unk.ad())));
                    string json = JsonSerializer.Serialize(dic);
                    //Console.WriteLine(json);
                    string miw = Code.b(json);
                    //Console.WriteLine(miw);

                    string outStr = "{\"success\":true,\"msg\":\"读取成功\",\"data\":\"" + miw + "\"}";
                    //Console.WriteLine(outStr);


                    // 处理请求，这里简单返回一个包含 "Hello, HTTP Service!" 的响应
                    string responseString = outStr;
                    byte[] buffer = Encoding.UTF8.GetBytes(responseString);

                    // 设置响应的内容长度和内容类型
                    response.ContentLength64 = buffer.Length;
                    response.ContentType = "text/html";

                    // 获取响应流并写入响应内容
                    Stream output = response.OutputStream;
                    output.Write(buffer, 0, buffer.Length);
                    // 关闭响应流
                    output.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"请求出错: {ex.Message}");
            }
        }

    }
}
